package qqserver.service;

import java.util.HashMap;
import java.util.Iterator;

/**
 * 该类用于管理和客户端通信的线程
 */
public class ManageClientThread {
    private static HashMap<String,ServerConnectClientThread> hashMap= new HashMap<>();

    //添加线程对象到 hashMap 集合
    public static void addClientThread(String userId, ServerConnectClientThread serverConnectClientThread){
        hashMap.put(userId, serverConnectClientThread);
    }

    //根据userId 返回 ServerConnectClientThread线程
    public static ServerConnectClientThread getServerConnectClientThread(String userId){
        return hashMap.get(userId);
    }

    //编写方法，从集合中移除某个线程对象
    public static void removeServerConnectClientThread(String userId){
        hashMap.remove(userId);
    }

    //编写方法，可以返回在线用户列表
    public static String getOnlineUser(){
        //遍历集合 hashMap 的key
        Iterator<String> iterator = hashMap.keySet().iterator();
        String onlineUserList = "";
        while (iterator.hasNext()){
            onlineUserList += iterator.next().toString() + " ";
        }
        return onlineUserList;
    }

    public static HashMap<String, ServerConnectClientThread> getHashMap() {
        return hashMap;
    }

}
